package com.sx.basic.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sx.basic.*;
import com.sx.basic.exception.BasicException;
import com.sx.basic.pageable.Condition;
import com.sx.basic.pageable.PageRequest;
import com.sx.redis.service.RedisService;
import lombok.Data;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author AdinZ
 * @date 2020/11/7 17:47
 */
public class BasicServiceImpl<M extends BaseMapper<T>,T extends BasicEntity> extends ServiceImpl<M,T>
        implements BasicService<T>, IBasicService<T, Long> {
    @Resource protected RedisService redisService;

    @Override
    public T get(Long id) {
        return super.getById(id);
    }

    @Override @Transactional
    public void delete(Long... id) {
        if(!super.removeByIds(Arrays.asList(id)))
            throw new BasicException("数据删除失败");
    }

    @Override @Transactional
    public void delete(Condition condition) {
        super.baseMapper.delete(new Query<T>(condition).getWrapper());
    }

    @Override @Transactional
    public T update(T t) {
        if(!super.updateById(t))
            throw new BasicException("数据更新失败");
        return t;
    }

    @Override @Transactional
    public void update(List<T> list) {
        if(!super.updateBatchById(list))
            throw new BasicException("数据更新失败");
    }

    @Override @Transactional
    public boolean save(T entity) {
      return  SqlHelper.retBool(this.baseMapper.insert(entity));
    }

    @Override @Transactional
    public void save(List<T> list) {
        if(!super.saveBatch(list))
            throw new BasicException("数据保存失败");
    }

    @Override
    public List<T> findList(Condition condition) {
        return super.list(new Query<T>(condition).getWrapper());
    }

    @Override
    public List<T> listById(Collection<Long> collection) {
        return super.listByIds(collection);
    }


    @Override
    public ResponseEntity<List<T>> findPage(int page, int size) {
        return this.findPage(PageRequest.of(page,size));
    }

    @Override
    public ResponseEntity<List<T>> findPage(PageRequest pageable) {
        Query<T> query=new Query<>(pageable);
        IPage<T> page=this.page(query.getPage(),query.getWrapper());
        return toPage(page);
    }

    /**
     *
     * @param condition 单层条件查询
     * @param pageable
     * @return
     */
    @Override
    public ResponseEntity<List<T>> findPage(Condition condition, PageRequest pageable) {
        Query<T> query=new Query<>(condition,pageable);
        IPage<T> page=this.page(query.getPage(),query.getWrapper());
        return toPage(page);
    }




    @Data
    private class Query<T> extends BasicEntity{
        private final Page<T> page;
        private final QueryWrapper<T> wrapper;
        public Query(PageRequest pageable){
            if(pageable!=null) {
                this.page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
            }else this.page=new Page<>();
            List<OrderItem> orders = page.getOrders();
            this.wrapper=new QueryWrapper<>();
            if(orders!=null&&orders.size()>0){
                for (OrderItem item:orders){
                    if(item.isAsc()) wrapper.orderByAsc(item.getColumn());
                    else wrapper.orderByDesc(item.getColumn());
                }
            }
        }
        public Query(Condition condition, PageRequest pageable){
            this(pageable);
            if(condition!=null&&!condition.isEmpty()){
                condition.forEach((k,v)->{
                    switch (v.getKeyword()){
                        case EQ:wrapper.eq(k,v.getValue());break;
                        case GE:wrapper.ge(k,v.getValue());break;
                        case GT:wrapper.gt(k,v.getValue());break;
                        case IN:wrapper.in(k,v.getValue());break;
                        case LE:wrapper.le(k,v.getValue());break;
                        case LT:wrapper.lt(k,v.getValue());break;
                        case NE:wrapper.ne(k,v.getValue());break;
                        case LIKE:wrapper.like(k,v.getValue());break;
                    }
                });
            }
        }
        public Query(Condition condition){
            this(condition,null);
        }
    }

    public <E>ResponseEntity<List<E>> toPage(IPage<E> page){
        ResponseEntity response=toSuccess("获取列表",page.getRecords());
        response.setTotal(page.getTotal());
        return response;
    }

}
